package com.etone.smartAudit.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etone.smartAudit.apiIntercept.Result;
import com.etone.smartAudit.domain.DataModelType;
import com.etone.smartAudit.domain.IapDataModel;
import com.etone.smartAudit.enums.IapModelDictType;
import com.etone.smartAudit.mapper.IapDataModelMapper;
import com.etone.smartAudit.utils.StringUtil;
import com.etone.smartAudit.vo.PagerVO;
import com.etone.smartAudit.vo.dataModelType.DataModelTypeTreeVO;
import com.etone.smartAudit.vo.dataModelType.DataModelTypeVO;
import com.etone.smartAudit.vo.iapDataModel.IapDateModelDictVO;
import com.etone.smartAudit.vo.iapDataModelNav.IapDataModelLevel2VO;
import com.etone.smartAudit.vo.iapDataModelNav.IapDataModelNavCountVO;
import com.etone.smartAudit.vo.iapDataModelNav.IapDataModelNavDetailVO;
import com.etone.smartAudit.vo.iapDataModelNav.IapDataModelNavVO;
import com.etone.smartFlow.common.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author ：张任豪
 * @date ：2022/2/21 14:12
 * @description：
 */
@Service
@AllArgsConstructor
public class IapDataModelNavService {

    private DataModelTypeService dataModelTypeService;

    private IapDataModelMapper iapDataModelMapper;

    private IapDataModelDictService iapDataModelDictService;

    /**
     * 元数据导航数据
     *
     * @param keyword
     * @param senLevel
     * @param dataAssertIds
     * @param dataMassIds
     * @param dimensionIds
     * @param autoTagIds
     * @param queryType
     * @return
     */
    public List<IapDataModelNavVO> list(String keyword, List<Integer> senLevel, List<Integer> dataAssertIds, List<Integer> dataMassIds, List<Integer> dimensionIds, List<Integer> autoTagIds, Integer queryType) {

        List<IapDataModelNavVO> list = new ArrayList<>();
        List<DataModelTypeTreeVO> treeVOS = dataModelTypeService.listTree();

        treeVOS.forEach(vos -> {
            IapDataModelNavVO navVO = new IapDataModelNavVO();
            List<IapDataModelLevel2VO> level2VOS = new ArrayList<>();
            //一级分类
            navVO.setName(vos.getName());
            navVO.setRemark(vos.getRemark());
            navVO.setCount(countType(vos.getId()));
            //二级分类
            if (!CollectionUtils.isEmpty(vos.getChildren())) {
                for (DataModelTypeTreeVO child : vos.getChildren()) {
                    IapDataModelLevel2VO level2VO = new IapDataModelLevel2VO();
                    level2VO.setName(child.getName());
                    level2VO.setRemark(child.getRemark());

                    //数据详情
                    String type = null;
                    if (queryType.equals(0)){
                        type = "and";
                    }else if (queryType.equals(1)){
                        type = "or";
                    }
                    List<IapDataModelNavDetailVO> navDetails = iapDataModelMapper.listNav(child.getId(), keyword, senLevel, dataAssertIds, dataMassIds, dimensionIds, autoTagIds,type);
                    if (!CollectionUtils.isEmpty(navDetails)) {
                        level2VO.setChildren(navDetails);
                        level2VOS.add(level2VO);
                    }
                }
            }
            if (!CollectionUtils.isEmpty(level2VOS)) {
                navVO.setChildren(level2VOS);
                list.add(navVO);
            }
        });
        return list;
    }

    /**
     * 分类统计
     *
     * @param level1
     * @return
     */
    public List<IapDataModelNavCountVO> countType(Integer level1) {
        //数据平台统计
        List<IapDataModelNavCountVO> navCountVOS = new ArrayList<>();
        int sum = 0;
        List<IapDateModelDictVO> dictVOS = iapDataModelDictService.list(IapModelDictType.DATA_PSSA.getValue());
        if (level1 != null) {
            if (!CollectionUtils.isEmpty(dictVOS)) {
                for (IapDateModelDictVO dictVO : dictVOS) {
                    Integer count = iapDataModelMapper.selectCount(new QueryWrapper<IapDataModel>()
                            .eq("intDataPssAId", dictVO.getId()).eq("intLevel1Id", level1));
                    navCountVOS.add(IapDataModelNavCountVO.builder()
                            .name(dictVO.getName()).amount(count).remark(dictVO.getRemark()).build());
                    sum += count;
                }
                navCountVOS.add(IapDataModelNavCountVO.builder()
                        .name("总计").amount(sum).build());
                return navCountVOS;
            }
        }

        //统计一级分类数据
        PagerVO<DataModelTypeVO> list = dataModelTypeService.list(0, 1, 1000);
        if (!CollectionUtils.isEmpty(list.getRows())) {
            for (DataModelTypeVO typeVO : list.getRows()) {
                Integer count = iapDataModelMapper.selectCount(new QueryWrapper<IapDataModel>()
                        .eq("intLevel1Id", typeVO.getId()));
                navCountVOS.add(IapDataModelNavCountVO.builder()
                        .name(typeVO.getName()).amount(count).remark(typeVO.getRemark()).build());
                sum += count;
            }
        }
        navCountVOS.add(IapDataModelNavCountVO.builder()
                .name("总计").amount(sum).build());
        return navCountVOS;
    }

    public Result testNav() {
        List<Integer> ids = Arrays.asList(17,23,32,36,44,43);
        List<IapDataModel> iapDataModels = iapDataModelMapper.selectList(new QueryWrapper<>());
        Random random = new Random();
        for (IapDataModel iapDataModel : iapDataModels) {
            DataModelType dataModelType = dataModelTypeService.getById(iapDataModel.getLevel2Id());
            if (dataModelType != null) {
                iapDataModel.setLevel2(dataModelType.getName());
            }else {
                iapDataModel.setLevel2Id(ids.get(random.nextInt(6)));
                iapDataModel.setLevel2("其他");
            }
            iapDataModelMapper.updateById(iapDataModel);
        }

        return null;
    }
}
